package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.template.LOrderbillTemplate;
import com.ruoyi.system.vo.LOrderReportVo;
import com.ruoyi.system.vo.LOrderReportlParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LOrderbill;
import com.ruoyi.system.service.ILOrderbillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单Controller
 *
 * @author zyb
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/system/orderbill")
public class LOrderbillController extends BaseController
{
    @Autowired
    private ILOrderbillService lOrderbillService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:list')")
    @GetMapping("/list")
    public TableDataInfo list(LOrderbill lOrderbill)
    {
        startPage();
        List<LOrderbill> list = lOrderbillService.selectLOrderbillList(lOrderbill);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LOrderbill lOrderbill)
    {
        List<LOrderbill> list = lOrderbillService.selectLOrderbillList(lOrderbill);
        ExcelUtil<LOrderbill> util = new ExcelUtil<LOrderbill>(LOrderbill.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:query')")
    @GetMapping(value = "/{billid}")
    public AjaxResult getInfo(@PathVariable("billid") Integer billid)
    {
        return AjaxResult.success(lOrderbillService.selectLOrderbillByBillid(billid));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LOrderbill lOrderbill)
    {
        return toAjax(lOrderbillService.insertLOrderbill(lOrderbill));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LOrderbill lOrderbill)
    {
        return toAjax(lOrderbillService.updateLOrderbill(lOrderbill));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderbill:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billids}")
    public AjaxResult remove(@PathVariable Integer[] billids)
    {
        return toAjax(lOrderbillService.deleteLOrderbillByBillids(billids));
    }


    @GetMapping(value = "/billcode")
    public AjaxResult getBillCode()
    {
        return AjaxResult.success(lOrderbillService.getBillCode());
    }

    @GetMapping(value = "/orderListNotExebilldetail")
    public AjaxResult orderListNotExebilldetail(Long areaId,Long billid)
    {
        if(areaId==null){
            return AjaxResult.error("请传区域Id,获取订单列表失败");
        }
        return AjaxResult.success(lOrderbillService.orderListNotExebilldetail(areaId,billid));
    }

    @Log(title = "订单导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:guest:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LOrderbillTemplate> util = new ExcelUtil<LOrderbillTemplate>(LOrderbillTemplate.class);
        List<LOrderbillTemplate> orderbillTemplateList = util.importExcel(file.getInputStream());
        String message = lOrderbillService.importOrder(orderbillTemplateList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LOrderbillTemplate> util = new ExcelUtil<LOrderbillTemplate>(LOrderbillTemplate.class);
        util.importTemplateExcel(response, "订单数据");
    }

    @PostMapping("/orderReport")
    public TableDataInfo orderReport(@RequestBody LOrderReportlParam orderReportlParam)
    {
        return getDataTable(lOrderbillService.orderReport(orderReportlParam));
    }
    @GetMapping("/orderNotWaybill")
    public AjaxResult orderNotWaybill(Long billid)
    {
        return AjaxResult.success(lOrderbillService.orderNotWaybill(billid));
    }
}
